package com.example.jixinlai.zyfypt_203jxl.model;




import com.example.jixinlai.zyfypt_203jxl.bean.SpecialBean;
import com.example.jixinlai.zyfypt_203jxl.iface.SpecialListener;
import com.example.jixinlai.zyfypt_203jxl.iface.Specialiface;
import com.example.jixinlai.zyfypt_203jxl.service.SpecialService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


//model 获取文章数据
public class SpecialModel implements Specialiface {
    private Retrofit retrofit;
    private String BASEURL="http://amicool.neusoft.edu.cn/";
    //构造函数
    public SpecialModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    //获取文章数据
    @Override
    public void getResultList(String mod, int page, String sessionID, final SpecialListener specialListener) {
        //使用Retrofit-2
        SpecialService specialService=retrofit.create(SpecialService.class);
    Call<List<SpecialBean>> call=specialService.getResult(mod,page,sessionID);
        //使用Retrofit-3
        call.enqueue(new Callback<List<SpecialBean>>() {
            @Override
            public void onResponse(Call<List<SpecialBean>> call, Response<List<SpecialBean>> response) {
                if(response.isSuccessful()&&response!=null){
                    specialListener.onResponse(response.body());
                }
                else
                    specialListener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<List<SpecialBean>> call, Throwable t) {
                specialListener.onFail(t.toString());
            }
        });
    }
}
